package com.polaris.live.dialog

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.AttachPopupView
import com.lxj.xpopup.enums.PopupAnimation
import com.lxj.xpopup.enums.PopupPosition
import com.polaris.live.R
import com.polaris.live.adapter.ManageItemAdapter
import com.polaris.live.adapter.user_space.UserSpaceGiftAdapter
import com.polaris.live.databinding.PopupManageSelectBinding
import com.polaris.live.databinding.PopupTogetherDetailsBinding
import com.polaris.live.resp.back_resp.ResourceOptionBaseVo
import com.polaris.live.resp.back_resp.UserConsumableRewardV2Resp
import com.polaris.live.resp.back_resp.UserMuteOptionsResp
import com.polaris.live.utils.route.RouterUtils

/**
 * ManageSelectDialog 选择
 *
 * @author Created by 天晴 on 2024/11/12 15:02
 * @since 1.0.0
 **/
class ManageSelectDialog(
    context: Context,
    private val data: List<ResourceOptionBaseVo>,
    private val width: Int,
    private val select: ((data: ResourceOptionBaseVo) -> Unit)?,
) : AttachPopupView(context) {

    private lateinit var mBinding: PopupManageSelectBinding
    override fun getImplLayoutId(): Int {
        return R.layout.popup_manage_select
    }

    private val mAdapter by lazy { ManageItemAdapter() }

    override fun onCreate() {
        mBinding = PopupManageSelectBinding.bind(popupImplView)
        val lp = mBinding.root.layoutParams
        lp.width = width
        mBinding.root.layoutParams = lp
        mBinding.recycler.apply {
            adapter = mAdapter
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            animation = null
        }
        mAdapter.setList(data)
        mAdapter.setOnItemClickListener { _, _, position ->
            select?.invoke(mAdapter.getItem(position))
            dismiss()
        }
    }


    companion object {

        fun show(
            context: Context,
            view: View,
            data: List<ResourceOptionBaseVo>,
            select: ((data: ResourceOptionBaseVo) -> Unit)?,
        ) {
            XPopup.Builder(context)
                .isViewMode(true)
                .moveUpToKeyboard(false)
                .isDestroyOnDismiss(true)
                .enableDrag(false)
                .atView(view)
                .popupPosition(PopupPosition.Bottom)
                .customAnimator(null)
                .popupAnimation(PopupAnimation.NoAnimation)
                .hasShadowBg(false)
                .asCustom(ManageSelectDialog(context, data, view.width, select))
                .show()
        }
    }
}

